package com.zhang.io;

import java.io.*;
import java.util.Base64;

/**
 * @Author 弯冬水
 * @Date 2023/2/25 11:44
 * @Description: TODO
 * @Version 1.0
 */
public class IOTest {
    public static void main(String[] args) throws Exception {
//        File file = new File("d://io/text.txt");
//        boolean mkdirs = file.mkdirs();
//        t1();
        t2();
//        copy();

    }

    private static void copy() throws IOException {
        File file1 = new File("D:\\Desktop\\1.png");
        File file = new File("D:\\Desktop\\2.png");
        copyFileUsingStream(file1,file);
    }

    private static void t2() throws IOException {
        File file1 = new File("D:\\Desktop\\1.png");
        File file = new File("D:\\Desktop\\2.png");
//        copyFileUsingStream(file1,file);


        FileInputStream fileInputStream = new FileInputStream(file1);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        FileOutputStream os = new FileOutputStream(file);
        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = fileInputStream.read())>0){
            os.write(buffer,0,len);
//            bos.write(buff,0,len);
        }

        os.close();

        byte[] bytes1 = bos.toByteArray();
        byte[] bytes = ImgUtils.compressPicByQuality(buffer, 0.5f);


        String base = Base64.getEncoder().encodeToString(bytes);
        System.out.println("data:image/png;base64,"+base);
        bos.close();
    }

    private static void t1() throws IOException {
        File file1 = new File("d://io/text.txt");
        boolean newFile = file1.createNewFile();
        FileOutputStream fileOutputStream = new FileOutputStream("d://io/text.txt");
        fileOutputStream.write("你是大傻逼阿达的".getBytes());
        fileOutputStream.close();

        FileReader fileReader = new FileReader("d://io/text.txt");
        int read;
        while ((read=fileReader.read())!=-1){
            System.out.println("read = " + read);
        }

        fileReader.close();
    }
    private static void copyFileUsingStream(File source, File dest) throws IOException {
        InputStream is = null;
        OutputStream os = null;
        try {
            is = new FileInputStream(source);
            os = new FileOutputStream(dest);
            byte[] buffer = new byte[1024];
            int length;
            while ((length = is.read(buffer)) > 0) {
                os.write(buffer, 0, length);
            }
        } finally {
            is.close();
            os.close();
        }
    }


}
